package com.feinno.urcs.group.util;

import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Li.Hongbo <lihongbo@feinno.com>
 */

public class FileTypeJudgeUtils {

	public static String getFileTypeWithPostfix(String fileName) {
		return new FileTypeJudgePostfix(fileName).getFileType();

	}

	public static String getFileTypeWithHeader(byte[] content) {
		return FileType.getTypeByHex(new FileTypeJudgeFileHeader(content).getFileType()).getPostfix();
	}

	public static void main(String[] args) throws Exception {
		System.out.println(FileTypeJudgeUtils.getFileTypeWithPostfix("asdjkajkduji233.mp4"));

		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		BufferedInputStream in = null;
		in = new BufferedInputStream(new FileInputStream("/Users/nickleo/Downloads/test_jpg.mp3"));
		int buf_size = 1024;
		byte[] buffer = new byte[buf_size];
		int len = 0;
		while (-1 != (len = in.read(buffer, 0, buf_size))) {
			bos.write(buffer, 0, len);
		}
		in.close();
		byte[] b = bos.toByteArray();
		bos.close();
		System.out.println(getFileTypeWithHeader(b));

	}


}

interface FileTypeJudge {
	public String getFileType();
}

class FileTypeJudgePostfix implements FileTypeJudge {

	private String filename = "";

	public FileTypeJudgePostfix(String fileName) {
		this.filename = fileName;
	}

	@Override
	public String getFileType() {
		if (!filename.contains("."))
			throw new IllegalArgumentException(String.format("filename[%s] no postfix ", this.filename));

		String[] names = filename.split("\\.");
		if (names.length < 2)
			throw new IllegalArgumentException(String.format("filename[%s] no postfix ", this.filename));

		return names[names.length - 1];
	}
}

class FileTypeJudgeFileHeader implements FileTypeJudge {

	private byte[] bytes;

	public FileTypeJudgeFileHeader(byte[] fileContent) {
		if (fileContent != null && fileContent.length > 3) {
			bytes = Arrays.copyOfRange(fileContent, 0, 3);
		}
	}

	private String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder();
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	@Override
	public String getFileType() {
		if (null == bytes || bytes.length < 3)
			throw new IllegalArgumentException("file content bytes  < 3 ");
		return bytesToHexString(bytes);
	}
}

enum FileType {
	OTHER_FILE("", ""),
	MIDI("mid", "4D546864"),
	JPEG("jpeg", "FFD8FF"),
	PNG("png", "89504E47"),
	Windows_Bitmap("bmp", "424D"),
	GIF_1("gif", "474946"),
	GIF("gif", "47494638"),
	Wave("wav", "57415645"),
	AVI("avi", "41564920"),
	Real_Audio("ram", "2E7261FD"),
	Real_Media("rm", "2E524D46"),
	MPEG_BA("mpg", "000001BA"),
	MPEG_B3("mpg", "000001B3"),
	Quicktime("mov", "6D6F6F76"),
	Windows_Media("asf", "3026B2758E66CF11"),

	Email("eml", "44656C69766572792D646174653A"),
	Outlook("pst", "2142444E"),
	Adobe_Acrobat("pdf", "255044462D312E"),
	ZIP_Archive("zip", "504B0304"),
	RAR_Archive("rar", "52617221"),;

	private FileType( String postfix,String hex) {
		this.hex = hex;
		this.postfix = postfix;
	}

	public static FileType getTypeByHex(String hex) {
		if (hex != null && hex.toLowerCase().length() > 0) {
			for (FileType t : FileType.values()) {

				if (t.getHex().toLowerCase().equals(hex.toLowerCase()))
					return t;
			}
		}
		return OTHER_FILE;
	}


	private String hex;
	private String postfix;

	public String getPostfix() {
		return postfix;
	}

	public String getHex() {
		return hex;
	}
}





